#! /usr/bin/env python
# -*- coding:utf-8 -*-
# author:无涯

import  requests
from box import  Box
import json

def writeToken(content):
	json.dump(content,open('token','w'))

def readToken():
	return json.load(open('token'))

def generate_token():
	r=requests.post(
		url='http://47.95.142.233:8000/login/auth/',
		json={"username":"13484545195","password":"asd888"})
	headers={'Authorization':'JWT {token}'.format(token=r.json()['token'])}
	writeToken(content=r.json()['token'])
	return Box(headers)

def headers():
	return {'Authorization':'JWT {token}'.format(token=readToken())}

def index_response(response):
	assert response.json()==json.load(open('qa.json'))['index']


def getProductID():
	r=requests.get(
		url='http://47.95.142.233:8000/interface/products?name=',
		headers=headers())
	productID=r.json()[0]['id']
	return productID


def generate_url_productID():
	url='http://47.95.142.233:8000/interface/product/{0}/'.format(getProductID())
	return Box(url)